package cn.org.timespace.easyframe.service.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.dao.permission.EfRoleMenuShortcutDAO;
import cn.org.timespace.easyframe.model.permission.EfMenu;
import cn.org.timespace.easyframe.model.permission.EfRole;
import cn.org.timespace.easyframe.model.permission.EfRoleShortcutmenu;
import cn.org.timespace.easyframe.service.permission.EfRoleMenuShortcutService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

/**
 * <p>
 * 角色快捷菜单
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author wubo
 */

public class EfRoleMenuShortcutServiceImpl implements EfRoleMenuShortcutService {

	private EfRoleMenuShortcutDAO efRoleMenuShortcutDao;

	public void delete(EfRoleShortcutmenu persistentInstance) {
		efRoleMenuShortcutDao.delete(persistentInstance);
	}

	public void delete(String roleId) {
		efRoleMenuShortcutDao.delete(roleId);
	}

	public List<EfRoleShortcutmenu> findByCondition(String roleId) {
		return efRoleMenuShortcutDao.findByCondition(roleId);
	}

	public void save(EfRoleShortcutmenu transientInstance) {
		efRoleMenuShortcutDao.save(transientInstance);
	}

	public EfRoleMenuShortcutDAO getEfRoleMenuShortcutDao() {
		return efRoleMenuShortcutDao;
	}

	public void setEfRoleMenuShortcutDao(
			EfRoleMenuShortcutDAO efRoleMenuShortcutDao) {
		this.efRoleMenuShortcutDao = efRoleMenuShortcutDao;
	}

	public void saveRoleMenuShortcut(String roleId, String menuShortcutIds) {

		if (!StringUtils.isEmpty(roleId)&& !StringUtils.isEmpty(menuShortcutIds)) {
			// 先删除
			efRoleMenuShortcutDao.delete(roleId);

			// 再保存
			String[] arrMenuIds = menuShortcutIds.split(",");
			for (int i = 0; i < arrMenuIds.length; i++) {
				EfRoleShortcutmenu efRoleMenu = new EfRoleShortcutmenu();
				efRoleMenu.setRoleShortcutmenuId(IdentityGenerator.getInstance().getId());

				EfRole efRole = new EfRole();
				efRole.setRoleId(roleId);
				efRoleMenu.setEfRole(efRole);

				EfMenu efMenu = new EfMenu();
				efMenu.setMenuId(arrMenuIds[i]);
				efRoleMenu.setEfMenu(efMenu);

				efRoleMenuShortcutDao.save(efRoleMenu);
			}
		}

	}

}
